package com.example.sunmiv1.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.RechargeRecordAdapter;
import com.example.sunmiv1.adapter.RechargeTypeAdapter;
import com.example.sunmiv1.base.BaseActivity;
import com.example.sunmiv1.entity.RechargeListTypeInfo;
import com.example.sunmiv1.entity.RechargeRecordInfo;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RechargeListActivity extends BaseActivity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.rechargeList_scene)
    TextView rechargeListScene;
    @BindView(R.id.rechargeList_mode)
    TextView rechargeListMode;
    @BindView(R.id.rechargeList_list)
    PullToRefreshListView rechargeListList;
    @BindView(R.id.rechargeList_dialog)
    LinearLayout rechargeListDialog;
    @BindView(R.id.rechargeList_typeList)
    ListView rechargeListTypeList;
    private ACache aCache;
    private Context context;
    private LoadingDialog loadingDialog;

    private int type = 0;//0:充值場景 1:到賬方式
    private List<RechargeListTypeInfo> sceneList = new ArrayList<>();
    private List<RechargeListTypeInfo> modeList = new ArrayList<>();
    private RechargeTypeAdapter rechargeTypeAdapter;

    private int page = 1;
    private RechargeRecordInfo rechargeRecordInfo;
    private List<RechargeRecordInfo.DataBean.ListBean> recordList = new ArrayList<>();
    private RechargeRecordAdapter rechargeRecordAdapter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_recharge_list);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.rechargeListTitle);
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
    }

    @Override
    protected void initData() {
        sceneList.add(new RechargeListTypeInfo("收銀機", false, 1));
        sceneList.add(new RechargeListTypeInfo("商戶後臺", false, 2));
        sceneList.add(new RechargeListTypeInfo("門店後臺", false, 3));
        sceneList.add(new RechargeListTypeInfo("小程序", false, 4));
        sceneList.add(new RechargeListTypeInfo("公衆號", false, 5));
        sceneList.add(new RechargeListTypeInfo("所有", false, 0));

        modeList.add(new RechargeListTypeInfo("直接到賬", false, 0));
        modeList.add(new RechargeListTypeInfo("分期到賬", false, 1));
        modeList.add(new RechargeListTypeInfo("所有", false, 2));
        getRechargeList();
    }

    private void setRechargeTypeAdapter() {
        if (type == 0) {
            rechargeTypeAdapter = new RechargeTypeAdapter(sceneList);
            rechargeListTypeList.setAdapter(rechargeTypeAdapter);
        } else {
            rechargeTypeAdapter = new RechargeTypeAdapter(modeList);
            rechargeListTypeList.setAdapter(rechargeTypeAdapter);
        }
    }

    private void setRechargeRecordAdapter() {
        if (rechargeRecordAdapter == null) {
            rechargeRecordAdapter = new RechargeRecordAdapter(recordList);
            rechargeListList.setAdapter(rechargeRecordAdapter);
        } else {
            rechargeRecordAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void initListener() {
        rechargeListTypeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (type == 0) {
                    sceneList.get(position).setCheck(!sceneList.get(position).isCheck());
                } else {
                    modeList.get(position).setCheck(!modeList.get(position).isCheck());
                }
                setRechargeTypeAdapter();
            }
        });
        rechargeListList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                recordList.clear();
                setRechargeRecordAdapter();
                getRechargeList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getRechargeList();
            }
        });
    }


    @OnClick({R.id.title_back, R.id.rechargeList_scene, R.id.rechargeList_mode, R.id.rechargeList_dialog})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.rechargeList_scene:
                type = 0;
                setRechargeTypeAdapter();
                rechargeListDialog.setVisibility(View.VISIBLE);
                break;
            case R.id.rechargeList_mode:
                type = 1;
                setRechargeTypeAdapter();
                rechargeListDialog.setVisibility(View.VISIBLE);
                break;
            case R.id.rechargeList_dialog:
                rechargeListDialog.setVisibility(View.GONE);
                page = 1;
                recordList.clear();
                setRechargeRecordAdapter();
                getRechargeList();
                break;
        }
    }

    private void getRechargeList() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        String scene_type = "";
        for (int i = 0; i < sceneList.size(); i++) {
            if (sceneList.get(i).isCheck()) {
                scene_type = scene_type + sceneList.get(i).getId() + ",";
            }
        }
        String repay_name = "";
        for (int i = 0; i < modeList.size(); i++) {
            if (modeList.get(i).isCheck()) {
                repay_name = repay_name + modeList.get(i).getId() + ",";
            }
        }
        try {
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            jsonObject.put("user_type", aCache.getAsString("userType"));
            jsonObject.put("searchText", "");
            jsonObject.put("scene_type", scene_type);
            jsonObject.put("repay_name", repay_name);
            jsonObject.put("page", page);
            jsonObject.put("limit", 20);
            HttpUtils.post(context, Common.VIP_RECHARGE_RECORD, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    rechargeListList.onRefreshComplete();
                    rechargeRecordInfo = GsonUtils.JsonToBean(text, RechargeRecordInfo.class);
                    if (rechargeRecordInfo.getStatus() == 1) {
                        page++;
                        recordList.addAll(rechargeRecordInfo.getData().getList());
                    }
                    setRechargeRecordAdapter();
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                    rechargeListList.onRefreshComplete();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
